<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <div>
            <span>全选:</span>
            <input v-model="selectAll" @change="SelectAll" type="checkbox"/>
            <button @click="Reverse">反选</button>
            <ul>
                <li v-for="(item,index) in arr" :key="index">
                    <input v-model="selectedItems" :value="item" type="checkbox" />
                    <span>{{item}}</span>
                </li>
            </ul>
        </div>
    </div>
    <script src="../vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                arr: [
                    // {
                    //     name: "猪八戒",
                    //     checked: false,
                    // },
                    // {
                    //     name: "孙悟空",
                    //     checked: false,
                    // },
                    // {
                    //     name: "唐僧",
                    //     checked: false,
                    // },
                    // {
                    //     name: "白龙马",
                    //     checked: false,
                    // },
                    "猪八戒","孙悟空","唐僧","白龙马"
                ],
                selectedItems:[],
                selectAll:false
            },
            methods:{
                SelectAll(){
                    if (this.selectAll){
                        this.selectedItems=[...this.arr]
                    }else{
                        this.selectedItems=[];
                    }
                },
                Reverse(){
                    this.selectedItems=this.arr.filter(item=>!this.selectedItems.includes(item));
                }
            },
            watch:{
                selectedItems:{
                    handler(newVal){
                        this.selectAll=newVal.length===this.arr.length;
                    },
                    deep:true
                }
            }
        });
    </script>


</body>

</html>